﻿using System.Collections.Generic;
using System.Linq;

namespace Ipmce.RemoteVoting.Common.Tools.CardReader.ImageParsing
{
	class FacialRecord : IBiometricDataBlock
	{
		public FacialRecordHeader FacialRecordHeader { get; set; }
		public List<FacialRecordData> FacialRecordDataList { get; set; }

		public int Parse(byte[] data, int offset)
		{
			offset = FacialRecordHeader.Parse(data, offset);
			for (var i = 0; i < FacialRecordHeader.NumberOfFacialImages; i++)
			{
				var item = new FacialRecordData();
				FacialRecordDataList.Add(item);
				offset = item.Parse(data, offset);
			}
			return offset;
		}

		public int Write(byte[] data, int offset)
		{
			offset = FacialRecordHeader.Write(data, offset);
			return FacialRecordDataList.Aggregate(offset, (current, item) => item.Write(data, current));
		}

		public int GetSize()
		{
			return FacialRecordHeader.GetSize() + FacialRecordDataList.Sum(p => p.GetSize());
		}

		public FacialRecord()
		{
			FacialRecordHeader = new FacialRecordHeader();
			FacialRecordDataList = new List<FacialRecordData>();
		}

		public FacialRecord(EnumFaceImageType faceImageType, ImageType imageDataType, int width, int height, byte[] imageData)
		{
			FacialRecordDataList = new List<FacialRecordData>
			                       	{new FacialRecordData(faceImageType, imageDataType, width, height, imageData)};
			FacialRecordHeader = new FacialRecordHeader(this);
		}
	}
}